﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

//@author A0090998H
namespace EZXPNS
{
    public partial class SearchPower : Form
    {
        public SearchPower()
        {
            InitializeComponent();
        }

        private void CloseASearch_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Confirm exit?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
                this.Close();

            else
            {
                //do nothing}
            };
        }

        private void SearchAdvance_Click(object sender, EventArgs e)
        {
            try
            {
                //Clear listView before searching
                listView1.Items.Clear();

                if (allTerms.Text == "" && exactPhrase.Text == "" && noneTerms.Text == ""
                    && priceFrom.Text == "" && priceTo.Text == "")
                {
                    MessageBox.Show("Nothing is entered!");
                    return;
                }

                if (EzXpns._incomeList.Count == 0 && EzXpns._expenseList.Count == 0)
                {
                    MessageBox.Show("No income and expense items!");
                    return;
                }

                string searchTermsAll, searchTermsNone;
                searchTermsAll = allTerms.Text;
                searchTermsNone = noneTerms.Text;

                string[] indivSearchTermsAll = searchTermsAll.Split(' ');
                string[] indivSearchTermsNone = searchTermsNone.Split(' ');

                int max = indivSearchTermsAll.Length;
                if (indivSearchTermsNone.Length > max)
                    max = indivSearchTermsNone.Length;
                max += 3; //to account for price and exact length


                //stores no. of matches per item
                int[] incomeItemAdded;
                incomeItemAdded = new int[EzXpns._incomeList.Count];
                int[] expenseItemAdded;
                expenseItemAdded = new int[EzXpns._expenseList.Count];

                //start EXACT
                if (exactPhrase.Text != "") //exact only got one phrase/ term, no need loop
                {
                    for (int j = 0; j < EzXpns._incomeList.Count; j++)
                    {
                        if (EzXpns._incomeList[j].Name.ToLower().Contains(exactPhrase.Text.ToLower())
                            || EzXpns._incomeList[j].Amount.ToString().ToLower().Contains(exactPhrase.Text.ToLower())
                            || EzXpns._incomeList[j].Category.ToLower().Contains(exactPhrase.Text.ToLower())
                            || EzXpns._incomeList[j].Payment.ToLower().Contains(exactPhrase.Text.ToLower())
                            )
                        {
                            incomeItemAdded[j] = 1;
                        }

                        //Loop through _expenseList
                        for (int m = 0; m < EzXpns._expenseList.Count; m++)
                        {
                            if (EzXpns._expenseList[m].Name.ToLower().Contains(exactPhrase.Text.ToLower())
                                || EzXpns._expenseList[m].Amount.ToString().ToLower().Contains(exactPhrase.Text.ToLower())
                                || EzXpns._expenseList[m].Category.ToLower().Contains(exactPhrase.Text.ToLower())
                                || EzXpns._expenseList[m].Payment.ToLower().Contains(exactPhrase.Text.ToLower())
                                )
                            {
                                expenseItemAdded[m] = 1;
                            }
                        }
                    }
                } //end EXACT

                //Search All 
                if (allTerms.Text != "")
                {
                    for (int i = 0; i < indivSearchTermsAll.Length; i++)
                    {

                        for (int j = 0; j < EzXpns._incomeList.Count; j++)
                        {
                            if (EzXpns._incomeList[j].Name.ToLower().Contains(indivSearchTermsAll[i].ToLower())
                                || EzXpns._incomeList[j].Amount.ToString().ToLower().Contains(indivSearchTermsAll[i].ToLower())
                                || EzXpns._incomeList[j].Category.ToLower().Contains(indivSearchTermsAll[i].ToLower())
                                || EzXpns._incomeList[j].Payment.ToLower().Contains(indivSearchTermsAll[i].ToLower())
                                )
                            {
                                incomeItemAdded[j]++;
                            }

                            //Loop through _expenseList
                            for (int m = 0; m < EzXpns._expenseList.Count; m++)
                            {
                                if (EzXpns._expenseList[m].Name.ToLower().Contains(indivSearchTermsAll[i].ToLower())
                                    || EzXpns._expenseList[m].Amount.ToString().ToLower().Contains(indivSearchTermsAll[i].ToLower())
                                    || EzXpns._expenseList[m].Category.ToLower().Contains(indivSearchTermsAll[i].ToLower())
                                    || EzXpns._expenseList[m].Payment.ToLower().Contains(indivSearchTermsAll[i].ToLower())
                                    )
                                {
                                    expenseItemAdded[m]++;
                                }
                            }
                        }
                    }

                } //end ALL

                //start PRICE
                if (allTerms.Text == "" && exactPhrase.Text == ""
                       && noneTerms.Text == "") //if only this field filled then print all in range
                {
                    for (int i = 0; i < EzXpns._incomeList.Count; i++)
                        incomeItemAdded[i] = 1;

                    for (int j = 0; j < EzXpns._expenseList.Count; j++)
                        expenseItemAdded[j] = 1;
                }

                if (priceFrom.Text != "" && priceTo.Text != "")
                {
                    for (int j = 0; j < EzXpns._incomeList.Count; j++)
                        if (EzXpns._incomeList[j].Amount < Decimal.Parse(priceFrom.Text)
                            || EzXpns._incomeList[j].Amount > Decimal.Parse(priceTo.Text))
                            incomeItemAdded[j] = 0;

                    for (int m = 0; m < EzXpns._expenseList.Count; m++)
                        if (EzXpns._expenseList[m].Amount < Decimal.Parse(priceFrom.Text)
                        || EzXpns._expenseList[m].Amount > Decimal.Parse(priceTo.Text))
                            expenseItemAdded[m] = 0;
                }

                else if (priceFrom.Text != "" && priceTo.Text == "")
                {
                    for (int j = 0; j < EzXpns._incomeList.Count; j++)
                        if (EzXpns._incomeList[j].Amount < Decimal.Parse(priceFrom.Text))
                            incomeItemAdded[j] = 0;

                    for (int m = 0; m < EzXpns._expenseList.Count; m++)
                        if (EzXpns._expenseList[m].Amount < Decimal.Parse(priceFrom.Text))
                            expenseItemAdded[m] = 0;
                }

                else if (priceFrom.Text == "" && priceTo.Text != "")
                {
                    for (int j = 0; j < EzXpns._incomeList.Count; j++)
                        if (EzXpns._incomeList[j].Amount > Decimal.Parse(priceTo.Text))
                            incomeItemAdded[j] = 0;

                    for (int m = 0; m < EzXpns._expenseList.Count; m++)
                        if (EzXpns._expenseList[m].Amount > Decimal.Parse(priceTo.Text))
                            expenseItemAdded[m] = 0;
                } //end PRICE


                //start NONE
                if (noneTerms.Text != "")
                {
                    if (allTerms.Text == "" && exactPhrase.Text == ""
                        && priceFrom.Text == "" && priceTo.Text == "") //print all that don't have the phrases
                    {
                        for (int i = 0; i < EzXpns._incomeList.Count; i++)
                            incomeItemAdded[i] = 1;

                        for (int j = 0; j < EzXpns._expenseList.Count; j++)
                            expenseItemAdded[j] = 1;
                    }

                    for (int i = 0; i < indivSearchTermsNone.Length; i++)
                    {

                        for (int j = 0; j < EzXpns._incomeList.Count; j++)
                        {
                            if (EzXpns._incomeList[j].Name.ToLower().Contains(indivSearchTermsNone[i].ToLower())
                                || EzXpns._incomeList[j].Amount.ToString().ToLower().Contains(indivSearchTermsNone[i].ToLower())
                                || EzXpns._incomeList[j].Category.ToLower().Contains(indivSearchTermsNone[i].ToLower())
                                || EzXpns._incomeList[j].Payment.ToLower().Contains(indivSearchTermsNone[i].ToLower())
                                )
                            {
                                incomeItemAdded[j] = 0; //remove item from list
                            }

                            //Loop through _expenseList
                            for (int m = 0; m < EzXpns._expenseList.Count; m++)
                            {
                                if (EzXpns._expenseList[m].Name.ToLower().Contains(indivSearchTermsNone[i].ToLower())
                                    || EzXpns._expenseList[m].Amount.ToString().ToLower().Contains(indivSearchTermsNone[i].ToLower())
                                    || EzXpns._expenseList[m].Category.ToLower().Contains(indivSearchTermsNone[i].ToLower())
                                    || EzXpns._expenseList[m].Payment.ToLower().Contains(indivSearchTermsNone[i].ToLower())
                                    )
                                {
                                    expenseItemAdded[m] = 0; //remove item from list
                                }
                            }
                        }
                    }

                } //end NONE

                //start DATE
                if (enableDate.Checked == true)
                {
                    DateTime StartTime = fromDate.Value;
                    DateTime EndTime = toDate.Value;

                    DateTime sTime = new DateTime(StartTime.Year, StartTime.Month, StartTime.Day);

                    DateTime eTime = new DateTime(EndTime.Year, EndTime.Month, EndTime.Day);


                    int expenseSize = expenseItemAdded.Length;
                    int incomeSize = incomeItemAdded.Length;

                    for (int i = 0; i < expenseSize; i++)
                    {
                        if (expenseItemAdded[i] >= 1)
                        {
                            DateTime temp = Convert.ToDateTime(EzXpns._expenseList[i].Date);
                            DateTime dateOnlyTemp = new DateTime(temp.Year, temp.Month, temp.Day);
                            if (dateOnlyTemp > eTime || dateOnlyTemp < sTime)
                            {
                                expenseItemAdded[i] = 0;
                            }
                        }
                    }
                    for (int i = 0; i < incomeSize; i++)
                    {
                        if (incomeItemAdded[i] >= 1)
                        {
                            DateTime temp = Convert.ToDateTime(EzXpns._incomeList[i].Date);
                            DateTime dateOnlyTemp = new DateTime(temp.Year, temp.Month, temp.Day);
                            if (dateOnlyTemp > eTime || dateOnlyTemp < sTime)
                            {
                                incomeItemAdded[i] = 0;
                            }
                        }
                    }
                }

                //end DATE



                //start "printing"
                for (int i = max; i >= 1; i--)
                {
                    for (int j = 0; j < EzXpns._incomeList.Count; j++)
                        if (incomeItemAdded[j] == i)
                        {
                            ListViewItem lvi = new ListViewItem("Income");
                            lvi.SubItems.Add(EzXpns._incomeList[j].Name);
                            lvi.SubItems.Add(EzXpns._incomeList[j].Amount.ToString());
                            lvi.SubItems.Add(EzXpns._incomeList[j].Category);
                            lvi.SubItems.Add(EzXpns._incomeList[j].Date);
                            lvi.SubItems.Add(EzXpns._incomeList[j].Payment);
                            listView1.Items.Add(lvi);
                        }

                    for (int m = 0; m < EzXpns._expenseList.Count; m++)
                        if (expenseItemAdded[m] == i)
                        {
                            ListViewItem lvi = new ListViewItem("Expense");
                            lvi.SubItems.Add(EzXpns._expenseList[m].Name);
                            lvi.SubItems.Add(EzXpns._expenseList[m].Amount.ToString());
                            lvi.SubItems.Add(EzXpns._expenseList[m].Category);
                            lvi.SubItems.Add(EzXpns._expenseList[m].Date);
                            lvi.SubItems.Add(EzXpns._expenseList[m].Payment);
                            listView1.Items.Add(lvi);
                        }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void enableDate_CheckedChanged(object sender, EventArgs e)
        {
            if (enableDate.Checked == true)
            {
                fromDate.Enabled = true;
                toDate.Enabled = true;
            }

            else
            {
                fromDate.Enabled = false;
                toDate.Enabled = false;
            }
        }
    }
}
